using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PACKETPARSERMAX20.CORE;

namespace PACKETPARSERMAX20.CORE.DEFINITIONS
{
    public class MSG_MOVE_TELEPORT_ACK_DEF : DefinitionBase
    {

        public override bool Process()
        {
            long current_position = base.Stream.Position;

            ProcessInternal(current_position);
   
            return Validate();
        }

        public override bool Process(uint pParentOpcode, uint opcode, long pPosition, long pSize)
        {
            base.SetCustomPositionAndSize(pPosition, pSize);
            ProcessInternal(pPosition);

            return ValidateMovementOpcode(pParentOpcode.ToString(), pPosition, pSize);
        }

        private void ProcessInternal(long pCurrentPosition)
        {
            base.Stream.Position = pCurrentPosition;

            MovementInfo movementInfo = new MovementInfo();

            if (base.Direction == 1)
            {
                movementInfo.guid = popGuid();

                UInt32 unk = ReadUInt32();

                movementInfo.movementFlags = (MovementFlags)ReadUInt32();

                if (ClientBuild >= 9183)
                {
                    movementInfo.movementFlags2 = (MovementFlags2)ReadUInt16();// 2.3.0
                }
                else
                {
                    movementInfo.movementFlags2 = (MovementFlags2)ReadByte();// 2.3.0
                }
 
                movementInfo.timeStamp = ReadUInt32();

                movementInfo.positionInfo = ReadCoord4();

                if ((movementInfo.movementFlags & MovementFlags.MOVEFLAG_ONTRANSPORT) != 0)
                {
                    movementInfo.transportInfo = ReadMovementTransport();
                }

                if (movementInfo.movementFlags.Value.HasAnyFlag(MovementFlags.MOVEFLAG_SWIMMING | MovementFlags.MOVEFLAG_FLYING) || movementInfo.movementFlags2.Value.HasFlag(MovementFlags2.MOVEFLAG2_ALLOW_PITCHING))
                {
                    movementInfo.swimPitch = ReadFloat();
                }

                movementInfo.fallTime = ReadUInt32();

                if (movementInfo.movementFlags.Value.HasFlag(MovementFlags.MOVEFLAG_FALLING))
                {
                    movementInfo.jumpInfo = ReadMovementJump();
                }

                if (movementInfo.movementFlags.Value.HasFlag(MovementFlags.MOVEFLAG_SPLINE_ELEVATION))
                {
                    movementInfo.splineelevation = ReadFloat();
                }

                UpdateObjectPosition(movementInfo);
            }
            else
            {
                if (ClientBuild >= 10314)
                {
                    movementInfo.guid = popGuid();
                }
                else
                {
                    movementInfo.guid = ReadUInt64();
                }
                float unk1 = ReadFloat();
                float unk2 = ReadFloat();

            }
        }
    }
}